package minidb.parse.exps;

import java.util.regex.*;
import minidb.querycondition.IOperand;

public class OperandString extends Operand implements IOperand
{
	public String strValue;

	public OperandString(String str)
	{
		this.strValue = str;
	}

	@Override
	public Boolean Equals(IOperand o)
	{
		if (OperandNull.class.isInstance(o))
			return false;
		return this.strValue.compareTo(((OperandString) o).strValue) == 0;
	}

	@Override
	public Boolean NotEq(IOperand o)
	{
		if (OperandNull.class.isInstance(o))
			return false;
		return !(this.Equals(o));
	}

	@Override
	public Boolean LittleThan(IOperand o)
	{
		if (OperandNull.class.isInstance(o))
			return false;
		return this.strValue.compareTo(((OperandString) o).strValue) < 0;
	}

	@Override
	public Boolean GreateThan(IOperand o)
	{
		if (OperandNull.class.isInstance(o))
			return false;
		return this.strValue.compareTo(((OperandString) o).strValue) > 0;
	}

	@Override
	public Boolean IsNull()
	{
		return false;
	}

	@Override
	public Boolean IsNotNull()
	{
		return true;
	}

	@Override
	public Boolean GreateEq(IOperand o)
	{
		if (OperandNull.class.isInstance(o))
			return false;
		return !(this.LittleThan(o));
	}

	@Override
	public Boolean LittleEq(IOperand o)
	{
		if (OperandNull.class.isInstance(o))
			return false;
		return !(this.GreateThan(o));
	}

	@Override
	public Boolean LIKE(IOperand o)
	{
		if (OperandNull.class.isInstance(o))
			return false;
		String pstr = ((OperandString) o).strValue;
		Pattern p = Pattern.compile(pstr);
		Matcher m = p.matcher(this.strValue);
		return m.matches();
	}

	@Override
	public Boolean NOTLIKE(IOperand o)
	{
		if (OperandNull.class.isInstance(o))
			return false;
		return !(this.LIKE(o));
	}

	// =======================================================================================

	@Override
	public String Print()
	{
		return "'" + this.strValue + "'";
	}

	@Override
	public String Print_Base()
	{
		return this.Print();
	}

	@Override
	public Operand DupOperand()
	{
		return new OperandString(this.strValue);
	}

	@Override
	public Object ToValue()
	{
		return this.strValue;
	}

}
